//
//  Index_liveCell.m
//  penzer
//
//  Created by caiyc on 2018/10/13.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "N_Index_liveCell.h"
#import "OYCountDownManager.h"
#define kCountDownNotification @"cutdown"
@implementation N_Index_liveCell{
    NSInteger countDwonTime;
    NSInteger endtime;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentVIews.layer.cornerRadius = 10;
    self.contentVIews.layer.masksToBounds = 1;
    self.goodImg.layer.cornerRadius = 5;
    self.goodImg.layer.masksToBounds = 1;
    
    self.live_type.layer.cornerRadius = 11;
    self.live_type.layer.masksToBounds = 1;
    self.checkdetailLab.layer.cornerRadius = 8;
    self.checkdetailLab.layer.masksToBounds = 1;
    self.check_rule_Btn.layer.cornerRadius = 12;
    self.check_rule_Btn.layer.masksToBounds = 1;
    self.sale_type.layer.cornerRadius = 11;
    self.sale_type.layer.masksToBounds = 1;
    self.time_botom_View1.layer.cornerRadius =self.time_botom_View1.frame.size.width/2;
    self.time_botom_View1.layer.masksToBounds = 1;
    self.time_bottom_View2.layer.cornerRadius = self.time_bottom_View2.frame.size.width/2;
    self.time_bottom_View2.layer.masksToBounds = 1;
    
   // self.hostIcon.layer.cornerRadius = CGRectGetMaxX(self.hostIcon.frame)/2;
    //self.hostIcon.layer.cornerRadius = self.hostIcon.frame.size.width;
    self.hostIcon.layer.cornerRadius = 12.5;
    self.hostIcon.layer.masksToBounds = 1;
    
    //self.live_Status.hidden = 1;
    
    self.hidden_View.backgroundColor = RGBA(0, 0, 0, .5);
    self.hidden_View.layer.cornerRadius = 5;
    self.hidden_View.layer.masksToBounds = 1;
    
    self.host_icon1.layer.cornerRadius=self.host_icon1.frame.size.height/2;
    self.percent.layer.cornerRadius=10;
    self.ablity.layer.cornerRadius = 10;
    self.percent.layer.masksToBounds = 1;
    self.ablity.layer.masksToBounds = 1;
    self.hidden_View.hidden = 1;
    self.liveStatuss_Lab.hidden = 1;
    
    // Initialization code
}
-(void)bindData:(NSDictionary *)liveData andtime:(nonnull NSString *)nowtime{
    self.maxNum.text = [NSString stringWithFormat:@"%@/%@",liveData[@"total_member"],liveData[@"member_max"]];
    NSDictionary *goodInfo = liveData[@"goods_list"][0];
    NSDictionary *userInfo = liveData[@"anchor_list"][0];
    self.goodName.text = goodInfo[@"title"];
    NSURL *iconUrl = [NSURL URLWithString:goodInfo[@"thumb"]];
    [self.goodImg sd_setImageWithURL:iconUrl];
    
    self.roomNum.text = [NSString stringWithFormat:@"房间：%@",liveData[@"room_number"]];
    NSString *price_text = [NSString stringWithFormat:@"¥%@元",goodInfo[@"buy_price"]];
    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Bold" size: 17],
                                NSForegroundColorAttributeName:PRICE_COLOR };
    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [price_text substringWithRange: NSMakeRange(0, price_text.length)] attributes: attrDict1];
    
    NSString *price_text1 = @"  直播价";
    NSDictionary *attrDict2 = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:PRICE_COLOR };
    NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: price_text1 attributes: attrDict2];
    NSMutableAttributedString *strs = [[NSMutableAttributedString alloc]init];
    
    
    NSString *price_Text = [NSString stringWithFormat:@"  ¥%@",goodInfo[@"price"]];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColor grayColor]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[price_Text substringWithRange: NSMakeRange(2, price_Text.length-2)] attributes:attribtDic];
    [strs appendAttributedString:attrStr1];
    [strs appendAttributedString:attrStr3];
    [strs appendAttributedString:attribtStr];
    self.price.attributedText = strs;
    // self.price.text =[NSString stringWithFormat:@"¥%@", goodInfo[@"buy_price"]];
    NSURL *hostIconUrl = [NSURL URLWithString:userInfo[@"avatar"]];
    [self.host_icon1 sd_setImageWithURL:hostIconUrl];
    [self.hostIcon sd_setImageWithURL:hostIconUrl placeholderImage:[UIImage imageNamed:@"Oval.png"]];
    
    self.hostNick.text = userInfo[@"nickname"];
    
    
    self.live_type.text = liveData[@"activity_cate_cn"];
  //  self.sale_type.text = liveData[@"live_type_cn"];
    self.live_type.text = liveData[@"live_type_cn"];
    NSURL *logo = [NSURL URLWithString:liveData[@"logo"]];
  //  NSURL *logo = [NSURL URLWithString:@"http://ph44nh83g.bkt.clouddn.com/images/10/2018/11/OU6RLeBSEU86l5BS87s5BBsZ7sSlO8.png"];
    [self.store_logo sd_setImageWithURL:logo];
    
    self.store_name.text = liveData[@"title"];
    NSArray *goods = liveData[@"goods_list"];
    self.goods_Num.text= [NSString stringWithFormat:@"%lu件商品",goods.count];
//    NSString *price_Text = [NSString stringWithFormat:@"市场价¥%@",goodInfo[@"price"]];
//    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColor grayColor]};
//    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[price_Text substringWithRange: NSMakeRange(0, price_Text.length)] attributes:attribtDic];
//    self.oldPrice.attributedText = attribtStr;
    
    NSString *timeStr = [XTool timeFrommartTotimeintral:liveData[@"start_time"]];
   // self.time.text = [NSString stringWithFormat:@"%@ 开始", timeStr];
    self.time.text = timeStr;
    
}
-(void)setModel:(LiveModel *)model{
    _model = model;
    if(model.count)
        [self countDownNotification];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    }
    return self;
}
// xib创建
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(countDownNotification)
                                                     name:TimeCellNotification_INDEX
                                                   object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
    }
    return self;
}
- (void)countDownNotification {
    LiveModel *model = self.model;
    NSInteger timeInterval;
    if(model.count<=0){
        self.liveStatuss_Lab.hidden = 0;
        self.liveStatuss_Lab.text = @"直播已结束";
        return;
    }
//    self.time_botom_View1.hidden = 0;
//    self.time_bottom_View2.hidden = 0;
//    self.mao.hidden = 0;
//    self.live_Status.hidden = 0;
    NSInteger countDown;
  //  NSLog(@"首页倒计时时间=%ld,=%ld",(long)model.startcount,(long)model.count);
    //第一次数据进来要做相应处理
    if(model.startcount>0){
        countDown =  model.startcount ;
        self.live_Status.text = @"后开始";
    }else{
        countDown = model.count ;
        self.live_Status.text = @"后结束";
    }
   [self getMMSSFromSS:countDown];
}
-(NSString *)getMMSSFromSS:(NSInteger )totalTime{
    
    NSInteger seconds = totalTime;
    NSInteger hour = seconds/3600;
    if(hour>0){
        self.time_botom_View1.hidden = 1;
        self.time_bottom_View2.hidden = 1;
      //  self.mao.hidden = 1;
        self.live_Status.hidden = 1;
        self.liveStatuss_Lab.hidden = 0;
        if(_model.startcount>0){
           //// self.liveStatuss_Lab.text = [NSString stringWithFormat:@"直播将于%ld小时后开始",(long)hour];
            self.liveStatuss_Lab.text =@"直播未开始";
        }else{
            self.liveStatuss_Lab.text = @"正在直播中";
        }
        
    }else{
        self.liveStatuss_Lab.hidden = 1;
    }
    NSString *str_hour = [NSString stringWithFormat:@"%ld",seconds/3600];
    //format of minute
    NSInteger minute = (seconds%3600)/60;
    NSString *str_minute = [NSString stringWithFormat:@"%ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    //format of minute
    // NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    if(str_minute.length>1){
        self.s_time.text = [str_minute substringWithRange:NSMakeRange(0, 1)];
        self.h_time.text =  [str_minute substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.s_time.text = @"0";
        self.h_time.text = str_minute;
    }
    if(str_second.length>1){
        self.s_time1.text = [str_second substringWithRange:NSMakeRange(0, 1)];
        self.s_time2.text =  [str_second substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.s_time1.text = @"0";
        self.s_time2.text = str_second;
    }
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];
    return format_time;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
